<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <!-- <el-form-item label="产品id" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品第三项目号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品第三项目号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品品牌" prop="productBrand">
        <el-input
          v-model="queryParams.productBrand"
          placeholder="请输入产品品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品酷家乐id" prop="productKjlId">
        <el-input
          v-model="queryParams.productKjlId"
          placeholder="请输入产品酷家乐id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品品类" prop="productCategory">
        <el-input
          v-model="queryParams.productCategory"
          placeholder="请输入产品品类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品分类" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="请选择产品分类" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品分类1名称" prop="productType1Name">
        <el-input
          v-model="queryParams.productType1Name"
          placeholder="请输入产品分类1名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品分类2名称" prop="productType2Name">
        <el-input
          v-model="queryParams.productType2Name"
          placeholder="请输入产品分类2名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品分类3名称" prop="productType3Name">
        <el-input
          v-model="queryParams.productType3Name"
          placeholder="请输入产品分类2名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="唯一ID" align="center" prop="id" /> -->
      <el-table-column label="产品图片" align="center" prop="productImg" min-width="120">
        <template slot-scope="scope">
          <img class="row-img" :src="scope.row.productImg"/>
        </template>
      </el-table-column>
      <el-table-column label="产品id" align="center" prop="productId"  min-width="200"/>
      <el-table-column label="产品名称" align="center" prop="productName"  min-width="200"/>
      <el-table-column label="产品第三项目号" align="center" prop="productCode"  min-width="200"/>
      <el-table-column label="产品品牌" align="center" prop="productBrand"  min-width="200"/>
      <el-table-column label="产品酷家乐模型名称" align="center" prop="productKjlName"  min-width="250"/>
      <el-table-column label="产品酷家乐id" align="center" prop="productKjlId"  min-width="200"/>
      <el-table-column label="产品品类" align="center" prop="productCategory"  min-width="200"/>
      <el-table-column label="产品分类" align="center" prop="productType"  min-width="200"/>
      <el-table-column label="产品分类1" align="center" prop="productType1"  min-width="200"/>
      <el-table-column label="产品分类1名称" align="center" prop="productType1Name"  min-width="200"/>
      <el-table-column label="产品分类2" align="center" prop="productType2"  min-width="200"/>
      <el-table-column label="产品分类2名称" align="center" prop="productType2Name"  min-width="200"/>
      <el-table-column label="产品分类3" align="center" prop="productType3"  min-width="200"/>
      <el-table-column label="产品分类2名称" align="center" prop="productType3Name"  min-width="200"/>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品模型上线对话框 -->
    <el-dialog class="dialog-table" title="产品上线" :visible.sync="open" width="1000px" append-to-body>
      <el-form :model="dialogParams" ref="dialogQueryForm" :inline="true" label-width="68px">
        <el-form-item label="产品名称" prop="productId">
          <el-input
            v-model="dialogParams.productId"
            placeholder="请输入产品名称或id"
            clearable
            size="small"
            @keyup.enter.native="handleDialogQuery"
          />

        </el-form-item>
        <el-form-item label="产品品牌" prop="udc01">
          <el-input
            v-model="dialogParams.udc01"
            placeholder="请输入产品品牌"
            clearable
            size="small"
            @keyup.enter.native="handleDialogQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleDialogQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetDialogQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dialogLoading" border stripe :data="dialogList" :highlight-current-row="true" @current-change="handleDialogSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="模型ID" align="center" prop="mId" />
        <el-table-column label="模型名称" align="center" prop="mName" />
        <el-table-column label="模型编号" align="center" prop="mDescription" />
        <el-table-column label="模型品牌" align="center" prop="udc01" />
        <el-table-column label="模型类型" align="center" prop="g2Class" />
        <el-table-column label="第三项目号" align="center" prop="prouductAitm" />
      </el-table>
      <pagination
        v-show="dialogTotal>0"
        :total="dialogTotal"
        :page.sync="dialogParams.pageNum"
        :limit.sync="dialogParams.pageSize"
        @pagination="getDialogList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改产品模型上线对话框 -->
    <el-dialog class="dialog-price" title="价格库存" :visible.sync="openPrice" width="1000px" append-to-body>
      <el-table border stripe :data="priceList">
          <el-table-column label="模型ID" align="center" prop="mId" />
      </el-table>
    </el-dialog>
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否有效" prop="isActive">
          <el-input v-model="form.isActive" placeholder="请输入是否有效" />
        </el-form-item>
        <el-form-item label="租户id" prop="lesseeId">
          <el-input v-model="form.lesseeId" placeholder="请输入租户id" />
        </el-form-item>
        <el-form-item label="产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品id" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品第三项目号" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品第三项目号" />
        </el-form-item>
        <el-form-item label="产品品牌" prop="productBrand">
          <el-input v-model="form.productBrand" placeholder="请输入产品品牌" />
        </el-form-item>
        <el-form-item label="产品酷家乐模型名称" prop="productKjlName">
          <el-input v-model="form.productKjlName" placeholder="请输入产品酷家乐模型名称" />
        </el-form-item>
        <el-form-item label="产品酷家乐id" prop="productKjlId">
          <el-input v-model="form.productKjlId" placeholder="请输入产品酷家乐id" />
        </el-form-item>
        <el-form-item label="产品图片" prop="productImg">
          <el-input v-model="form.productImg" placeholder="请输入产品图片" />
        </el-form-item>
        <el-form-item label="产品品类" prop="productCategory">
          <el-input v-model="form.productCategory" placeholder="请输入产品品类" />
        </el-form-item>
        <el-form-item label="产品分类">
          <el-select v-model="form.productType" placeholder="请选择产品分类">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="产品分类1" prop="productType1">
          <el-input v-model="form.productType1" placeholder="请输入产品分类1" />
        </el-form-item>
        <el-form-item label="产品分类1名称" prop="productType1Name">
          <el-input v-model="form.productType1Name" placeholder="请输入产品分类1名称" />
        </el-form-item>
        <el-form-item label="产品分类2" prop="productType2">
          <el-input v-model="form.productType2" placeholder="请输入产品分类2" />
        </el-form-item>
        <el-form-item label="产品分类2名称" prop="productType2Name">
          <el-input v-model="form.productType2Name" placeholder="请输入产品分类2名称" />
        </el-form-item>
        <el-form-item label="产品分类3" prop="productType3">
          <el-input v-model="form.productType3" placeholder="请输入产品分类3" />
        </el-form-item>
        <el-form-item label="产品分类2名称" prop="productType3Name">
          <el-input v-model="form.productType3Name" placeholder="请输入产品分类2名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listProduct, getProduct, getProductInfo, delProduct, addProduct, updateProduct, exportProduct, getKjlModelList } from "@/api/design/product";

export default {
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      dialogLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 产品模型上线表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: undefined,
        productName: undefined,
        productCode: undefined,
        productBrand: undefined,
        productKjlId: undefined,
        productCategory: undefined,
        productType: undefined,
        productType1Name: undefined,
        productType2Name: undefined,
        productType3Name: undefined
      },
      dialogParams:{
        pageNum: 1,
        pageSize: 10,
        productId: undefined,
        udc01: undefined
      },
      dialogTotal:0,
      dialogList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      openPrice:false,
      priceList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品模型上线列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDialogList(){
      this.dialogLoading = true
      getKjlModelList(this.dialogParams).then(response => {
        this.dialogList = response.rows;
        this.dialogTotal = response.total;
        this.dialogLoading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isActive: undefined,
        lesseeId: undefined,
        productId: undefined,
        productName: undefined,
        productCode: undefined,
        productBrand: undefined,
        productKjlName: undefined,
        productKjlId: undefined,
        productImg: undefined,
        productCategory: undefined,
        productType: undefined,
        productType1: undefined,
        productType1Name: undefined,
        productType2: undefined,
        productType2Name: undefined,
        productType3: undefined,
        productType3Name: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleDialogQuery() {
      this.dialogParams.pageNum = 1;
      this.getDialogList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    resetDialogQuery() {
      this.resetForm("dialogQueryForm");
      this.handleDialogQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    handleDialogSelectionChange(currentRow, oldCurrentRow) {
      console.log(currentRow)
      if(currentRow.udc01 != '华耐软装'){
        this.form = {
          productId: currentRow.productId||'',
          productName: currentRow.mName||'',
          productCode: currentRow.prouductAitm||'',
          productBrand: currentRow.udc01||'',
          productKjlName: currentRow.mName||'',
          productKjlId: currentRow.mId||'',
          productImg: currentRow.imgurl||'',
          productCategory: currentRow.g1Class||'',
          productType: currentRow.mType||'',
          productType1: '',
          productType1Name: currentRow.g2Class||'',
          productType2:'',
          productType2Name:'',
          productType3:'',
          productType3Name:'',
        }
      }else if(currentRow.mType == '软装'){
        this.form = {
          productId: currentRow.mDescription||'',
          productName: currentRow.mName||'',
          productCode: currentRow.mDescription||'',
          productBrand: currentRow.udc01||'',
          productKjlName: currentRow.mName||'',
          productKjlId: currentRow.mId||'',
          productImg: currentRow.imgurl||'',
          productCategory: '软装',
          productType: currentRow.mType||'',
          productType1: '',
          productType1Name: '',
          productType2:'',
          productType2Name:'',
          productType3:'',
          productType3Name:'',
        }
      }

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.resetDialogQuery()
      //this.title = "添加产品模型上线";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        //this.title = "修改产品模型上线";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      if(this.dialogLoading){
        return
      }
      if(!this.form.productId){
        if(this.form.productName){
          this.msgError("当前商品未维护ID");
        }else{
          this.msgError("请选择上线的商品");
        }
        return
      }
      this.dialogLoading = true
      listProduct({productId:this.form.productId}).then(res => {
        if(res.rows && res.rows.length){
          this.msgError("该商品已上线");
          this.dialogLoading = false
        }else{
          addProduct(this.form).then(response => {
            this.dialogLoading = false
            if (response.code === 200) {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }
          }).catch(()=>{
            this.dialogLoading = false
          });
        }
      }).catch(()=>{
        this.dialogLoading = false
      });
      /* if (this.form.id != undefined) {
        updateProduct(this.form).then(response => {
          if (response.code === 200) {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          }
        });
      } else {
        addProduct(this.form).then(response => {
          if (response.code === 200) {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }
        });
      } */
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除产品模型上线编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProduct(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品模型上线数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProduct(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },

    handleView(row){
      this.openPrice = true
      getProductInfo()
    }
  }
};
</script>
<style scoped>
  .row-img{
    width: 80px;
    height: 80px;
  }
  .dialog-table /deep/ .current-row td{
    background-color: #1890ff;
    color: #FFFFFF;
  }
  .dialog-table /deep/ .current-row.el-table__row--striped td{
    background-color: #1890ff !important;
    color: #FFFFFF;
  }
</style>



